//
//  UPSCheckViewController.m
//  Qihuo
//
//  Created by Vencoo on 14-1-3.
//  Copyright (c) 2014年 Vencoo. All rights reserved.
//

#import "UPSCheckViewController.h"
#import "CheckUPSCell.h"
@interface UPSCheckViewController ()
{
    NSMutableArray *_floorNames;
    NSMutableArray *_floorRemarks;
    NSMutableArray *_floorResults;
    UITableView *_mainTable;
}
@end

@implementation UPSCheckViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"UPS巡检记录";
    NSString *_checkURL = resultOfUPS;
    NSURL *queryUrl = [NSURL URLWithString:_checkURL];
    ASIFormDataRequest *obtainFloor = [ASIFormDataRequest requestWithURL:queryUrl];
    [obtainFloor setPostValue:self.delegate.sendCheckTime forKey:@"checkTime"];
    [obtainFloor startSynchronous];
    
    NSString *response = [obtainFloor responseString];
    NSDictionary *queryDic = [response JSONValue];
    NSDictionary *queryDataArr = [queryDic objectForKey:@"data"];
    NSArray *floorData = [queryDataArr objectForKey:@"floorData"];
    
    NSLog(@"查询%@",floorData);
    if (floorData.count >0 &&[[floorData firstObject]valueForKey:@"floorName"])
    {
        _floorNames = [NSMutableArray array];
    }
    if (floorData.count >0 &&[[floorData firstObject]valueForKey:@"remark"])
    {
        _floorRemarks = [NSMutableArray array];
    }
    if (floorData.count >0 &&[[floorData firstObject]valueForKey:@"result"])
    {
        _floorResults = [NSMutableArray array];
    }
    [self showMainView:floorData];

    for (int i = 0; i < floorData.count; i ++)
    {
        NSDictionary *queryDic = [floorData objectAtIndex:i];
        if ([queryDic objectForKey:@"floorName"]) {
            [_floorNames addObject:[queryDic objectForKey:@"floorName"]];
        }
        if ([queryDic objectForKey:@"remark"]) {
            [_floorRemarks addObject:[queryDic objectForKey:@"remark"]];
        }
        if ([queryDic objectForKey:@"result"]) {
            [_floorResults addObject:[queryDic objectForKey:@"result"]];
        }
        
    }
}


- (void)showMainView:(NSArray *)floorData
{
    UIImage *lineImage = [UIImage imageNamed:@"黑边框"];
    UIImage *whiteimage = [UIImage imageNamed:@"底框-白.png"];
    UIImageView *MyView = [[UIImageView alloc]initWithFrame:CGRectMake((scrennWidth - whiteimage.size.width)/2, 130, whiteimage.size.width, whiteimage.size.height + 30)];
    MyView.image = whiteimage;
    [MyView setUserInteractionEnabled:YES];
    [self.view addSubview:MyView];
    
    UIImageView *lineView = [[UIImageView alloc]initWithImage:lineImage];
    lineView.frame = CGRectMake((whiteimage.size.width - lineImage.size.width)/2, (whiteimage.size.height - lineImage.size.height)/2 + 30, lineImage.size.width, lineImage.size.height);
    lineView.userInteractionEnabled = YES;
    [MyView addSubview:lineView];
    
    NSString *hostStatus = [[floorData firstObject]valueForKey:@"hostStatus"];
    NSString *softwareStatus = [[floorData firstObject]valueForKey:@"softwareStatus"];
    NSString *communicationStatus = [[floorData firstObject]valueForKey:@"communicationStatus"];

    for (int i = 0; i<3; i ++)
    {
        UILabel *headTitleLb = [UILabel new];
        UILabel *TF = [UILabel new];
        switch (i)
        {
            case 0:
                headTitleLb.text = @"主机状况：";
                TF.text = hostStatus;
                break;
            case 1:
                headTitleLb.text = @"软件状况：";
                TF.text = softwareStatus;
                break;
            case 2:
                headTitleLb.text = @"通讯状况：";
                TF.text = communicationStatus;
                break;
                
            default:
                break;
        }
        headTitleLb.textColor = [UIColor whiteColor];
        headTitleLb.font = [UIFont systemFontOfSize:30];
        CGSize mySz = [headTitleLb.text sizeWithFont:headTitleLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
        headTitleLb.frame = CGRectMake(interval111 + (scrennWidth - interval111*2)/3 * i, 80, mySz.width,mySz.height);
        [self.view addSubview:headTitleLb];
        
        TF.frame = CGRectMake(headTitleLb.frame.origin.x + headTitleLb.frame.size.width, headTitleLb.frame.origin.y, 150, mySz.height);
        TF.font = [UIFont systemFontOfSize:30];
        TF.textColor = [UIColor whiteColor];
        [self.view addSubview:TF];
    }
    
    UILabel *floorLb = [UILabel new];
    floorLb.text = @"楼层";
    floorLb.font = [UIFont systemFontOfSize:30];
    CGSize mysz = [floorLb.text sizeWithFont:floorLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    floorLb.frame = CGRectMake(lineView.frame.origin.x + 20, (lineView.frame.origin.y - mysz.height)/2, mysz.width, mysz.height);
    [MyView addSubview:floorLb];
    NSArray *_checkNames = @[@"备电时间",@"备注"];

    for (int i = 0; i < _checkNames.count; i ++)
    {
        UILabel *floorLbNew = [UILabel new];
        floorLbNew.text = [_checkNames objectAtIndex:i];
        floorLbNew.font = [UIFont systemFontOfSize:20];
        mysz = [[NSString stringWithFormat:@"啊啊啊啊"] sizeWithFont:floorLbNew.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
        floorLbNew.numberOfLines = 0;
        floorLbNew.textAlignment = 1;
        if (i == 0)
        {
            floorLbNew.frame = CGRectMake((MyView.frame.size.width - 40 - floorLb.frame.size.width)/_checkNames.count*i+((lineView.frame.size.width - 40 - floorLb.frame.size.width)/_checkNames.count-mysz.width)/2 + floorLb.frame.origin.x + floorLb.frame.size.width-50, floorLb.frame.origin.y,mysz.width, mysz.height*2);
        }
        else
        {
            floorLbNew.frame = CGRectMake((MyView.frame.size.width - 40 - floorLb.frame.size.width)/_checkNames.count*i+((lineView.frame.size.width - 40 - floorLb.frame.size.width)/_checkNames.count-mysz.width)/2 + floorLb.frame.origin.x + floorLb.frame.size.width - 500, floorLb.frame.origin.y + mysz.height/2,(MyView.frame.size.width - 40 - floorLb.frame.size.width)/_checkNames.count, mysz.height*(1 + mysz.width/(MyView.frame.size.width - 40 - floorLb.frame.size.width)/_checkNames.count));
        }
        [MyView addSubview:floorLbNew];
    }
    _mainTable = [[UITableView alloc]initWithFrame:CGRectMake(2, 5, lineImage.size.width-6, lineImage.size.height-8) style:UITableViewStylePlain];
    _mainTable.dataSource = self;
    _mainTable.delegate = self;
    [lineView addSubview:_mainTable];
    _mainTable.tag = 100;
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _floorNames.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = [NSString stringWithFormat:@"cell%d",indexPath.row];
    CheckUPSCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell)
    {
        cell = [[CheckUPSCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (_floorNames.count > 0) {
        cell.longFloorNameLb.text = [_floorNames objectAtIndex:indexPath.row];
    }
    cell.resultLb.text = [_floorResults objectAtIndex:indexPath.row];
    if (_floorRemarks.count > 0) {
        cell.processLb.text = [_floorRemarks objectAtIndex:indexPath.row];
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
